package pers.zb.cloud.web.upms.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import pers.zb.cloud.web.upms.entity.ClientDetail;
import pers.zb.cloud.web.upms.service.ClientDetailService;
import pers.zb.cloud.web.upms.vo.JsonResult;
import pers.zb.cloud.web.upms.vo.Page;

/**
 * 客户端应用服务
 */
@Slf4j
@Controller
@RequestMapping("/client")
public class ClientController {
    @Autowired
    private ClientDetailService clientDetailService;


    /**
     * 应用列表页面
     */
    @RequestMapping(value = "/view/list", method = {RequestMethod.GET})
    public String view() {
        return "/sys/client/list";
    }

    /**
     * 分页获取应用列表
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Page<ClientDetail> getClientList(Page pageParam){
        Page<ClientDetail> clientDetailList = clientDetailService.getClientListForPage(pageParam);
        return clientDetailList;
    }

    /**
     * 进入应用修改页面
     */
    @RequestMapping(value = "/{clientId}", method = {RequestMethod.PUT})
    public ModelAndView edit(@PathVariable("clientId") String clientId) {
        ClientDetail clientDetail = clientDetailService.getClientDetailByClientId(clientId);

        ModelAndView modelAndView = new ModelAndView("/sys/client/edit");
        modelAndView.addObject("clientDetail",clientDetail);
        return modelAndView;
    }

    /**
     * 更新、添加应用
     *      type： 1 - 更新   2 - 添加
     */
    @RequestMapping(value = "/{type}", method = {RequestMethod.POST})
    @ResponseBody
    public JsonResult<String> updateOrSave(@PathVariable("type") int type, ClientDetail clientDetail) {
        JsonResult<String> jsonResult = new JsonResult<>();
        try {
            clientDetailService.updateOrSave(type,clientDetail);
        } catch (Exception e) {
            log.debug("更新 or 添加应用 - 失败：" + e.getMessage());
            jsonResult.setCode(500);
            jsonResult.setMsg(e.getMessage());
            return jsonResult;
        }
        return jsonResult;
    }

    /**
     * 进入新增应用页面
     */
    @RequestMapping(value = "/view/add", method = {RequestMethod.GET})
    public ModelAndView addClientVew() {
        ModelAndView modelAndView = new ModelAndView("/sys/client/add");
        return modelAndView;
    }


    /**
     * 删除应用
     */
    @RequestMapping(value = "/batch", method = {RequestMethod.POST})
    @ResponseBody
    public JsonResult<String> delete(@RequestParam(value = "clientId") String clientId) {
        JsonResult<String> jsonResult = new JsonResult<>();
        try {
            String[] clientIdArrString = clientId.split(",");
            clientDetailService.deleteClient(clientIdArrString);
        } catch (Exception e) {
            log.debug("删除应用 - 失败：" + e.getMessage());
            jsonResult.setCode(500);
            jsonResult.setMsg(e.getMessage());
            return jsonResult;
        }
        return jsonResult;
    }
}
